package com.basics.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.basics.common.utils.http.HttpRequest;
import com.basics.common.utils.http.HttpUtils;
import com.basics.framework.config.BasicsConfig;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";

    private static final String tx_key = "I6LBZ-NSXW3-YNU3Y-YQZWP-BZX6Q-3JFGM";

    private static final String location = "https://apis.map.qq.com/ws/geocoder/v1/?key=I6LBZ-NSXW3-YNU3Y-YQZWP-BZX6Q-3JFGM&location=";

    private static final String ipLOcation = "https://apis.map.qq.com/ws/location/v1/ip?key=I6LBZ-NSXW3-YNU3Y-YQZWP-BZX6Q-3JFGM&ip=";

    public AddressUtils() {
    }

    public static String getRealAddressByIP(String ip) {
        String address = "XX XX";
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        } else {
            if (BasicsConfig.isAddressEnabled()) {
                String rspStr = HttpUtils.sendPost("http://ip.taobao.com/service/getIpInfo.php", "ip=" + ip);
                if (StringUtils.isEmpty(rspStr)) {
                    log.error("获取地理位置异常 {}", ip);
                    return address;
                }

                JSONObject obj = JSONObject.parseObject(rspStr);
                JSONObject data = (JSONObject)obj.getObject("data", JSONObject.class);
                String region = data.getString("region");
                String city = data.getString("city");
                address = region + " " + city;
            }

            return address;
        }
    }

    public static String getAddressByLocation(String lat,String lng,String ip){
        if(StringUtils.isNotEmpty(lat)&&StringUtils.isNotEmpty(lng)){
            try{
                String res = HttpRequest.sendGet(location+lat+","+lng);
                JSONObject jo = JSONObject.parseObject(res);
                return jo.getJSONObject("result").getString("address");
            }catch (Exception e){

            }
        }
        return getAddressByIp(ip);
    }

    public static String getAddressByIp(String ip){
        if(StringUtils.isNotEmpty(ip)){
            try{
                String res = HttpRequest.sendGet(ipLOcation+ip);
                JSONObject jo = JSONObject.parseObject(res);
                log.error("获取到的地址信息:{},IP:{}",jo,ip);
                JSONObject so = jo.getJSONObject("result").getJSONObject("ad_info");
                return so.getString("province")+" "+so.getString("city")+(so.containsKey("district")?" "+so.getString("district"):"");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "未知";
    }

    public static void main(String[] args) throws Exception{
        // 创建连接对象并指定URL
        Connection connection = Jsoup.connect("https://www.7mjs.com/yingshijieshuo/dongzuofanzui");

        // 添加需要的Cookie信息（这里只作为示例）
        connection.header("Cookie", "PHPSESSID=gscfqjq3h986scla9tr9kbb313; Hm_lvt_0a977834ddca371197cc832fd2849b93=1706463311; wordpress_logged_in_74f4134518af3e7629e6a899eba5b0b9=mail_22352018%7C1707673076%7CzqeCJTtdmOiXp5ZbbuY5XFAmItm3s1JOqVIXEa9GWmY%7C60732bff9d05d7f2829a9f6fbc780de03d70940e435fa54fdb1a6bc4a1e8fa9c; Hm_lpvt_0a977834ddca371197cc832fd2849b93=1706465727");

        // 发起GET请求获取页面内容
        String htmlContent = connection.get().html();

        System.out.println(htmlContent);

    }
}

